package com.whcoding.base.project.common.api;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * <p>
 * 统一API对象返回
 * </p>
 *
 * @author whcoding
 * @date Created in 2021-10-25 23:10
 */
@Data
@NoArgsConstructor
public class BaseResultVO<T> implements Serializable {
	/**
	 * 状态码
	 */
	private Integer code;

	/**
	 * 返回消息
	 */
	private String message;

	/**
	 * 状态
	 */
	private boolean status;

	/**
	 * 返回数据
	 */
	private T data;

	public BaseResultVO(Integer code, String message, boolean status, T data) {
		this.code = code;
		this.message = message;
		this.status = status;
		this.data = data;
	}

	public BaseResultVO(IResultCode resultCode, boolean status, T data) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
		this.status = status;
		this.data = data;
	}

	public BaseResultVO(IResultCode resultCode, boolean status) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
		this.status = status;
		this.data = null;
	}

	public BaseResultVO(IResultCode resultCode,String message, boolean status) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
		this.status = status;
		this.data = null;
	}


	/**
	 * 默认成功
	 * @param <T>
	 * @return
	 */
	public static <T> BaseResultVO success() {
		return new BaseResultVO<>(ResultStatusEnum.SUCCESS, ResultStatusEnum.SUCCESS.getMessage(),true);
	}

	public static <T> BaseResultVO message(String message) {
		return new BaseResultVO<>(ResultStatusEnum.SUCCESS.getCode(), message, true, null);
	}

	public static <T> BaseResultVO success(T data) {
		return new BaseResultVO<>(ResultStatusEnum.SUCCESS, true, data);
	}

	/**
	 * 默认返回失败
	 * @param <T>
	 * @return
	 */
	public static <T> BaseResultVO fail() {
		return new BaseResultVO<>(
				ResultStatusEnum.ERROR,
				ResultStatusEnum.ERROR.getMessage(),
				false);
	}

	public static <T> BaseResultVO fail(IResultCode resultCode) {
		return new BaseResultVO<>(resultCode, false);
	}

	public static <T> BaseResultVO fail(Integer code, String message) {
		return new BaseResultVO<>(code, message, false, null);
	}

	public static <T> BaseResultVO fail(IResultCode resultCode, T data) {
		return new BaseResultVO<>(resultCode, false, data);
	}

//	public static <T> BaseResultVO fail(Integer code, String message, T data) {
//		return new BaseResultVO<>(code, message, false, data);
//	}

	/**
	 * 针对自定义异常返回
	 * @param code
	 * @param message
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T> BaseResultVO fail(Integer code, String message, Object data) {
		return new BaseResultVO<>(code, message, false, data);
	}
}
